#pragma once
#include <iostream>
#include <string>
#include "Protocol.hpp"
#include"log.hpp"
using namespace LogMudule;
class Cal
{
public:
  Cal()
  {
  }
  ~Cal()
  {
  }
  // req x y oper
  Respond Excute(const Request &req)
  {
    Respond resp;
    switch (req.Oper())
    {
    case '+':
      resp.SetResult(req.X() + req.Y());
      break;
    case '-':
      resp.SetResult(req.X() - req.Y());
      break;
    case '*':
      resp.SetResult(req.X() * req.Y());
      break;
    case '/':
    {
      if (req.Y() == 0)
      {
        resp.SetCode(1);
      }
      else
      {
        resp.SetResult(req.X() / req.Y());
      }
    }
    break;
    case '%':
      {
        if(req.Y()==0)
        {
          resp.SetCode(2);          
        }
        else
        {
          resp.SetResult(req.X() % req.Y());
        }
      }    
      break;
    default:
      resp.SetCode(3);
      LOG(LogLevel::ERROR)<<"无法识别操作符";
      break;
    }
    LOG(LogLevel::DEBUG)<<"返回计算结果"<<resp.Result()<<resp.Code();
    return resp;
  }
};
